import json
from time import sleep
import allure
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
from common.commin.commin import object_left_click, read_ini
from common.excel.excel import read_all_caseexcel
from common.log.log import log
from common.request.request_api import RequestHandler
from config.conf import BASE_DIR
from po.jinhua.ht.jinhua_ht_main.jinhua_base import JHBase


class JHHTHomePage(JHBase):
    """
    金花商城后台主页
    """

    @allure.step('进入商品管理菜单页')
    def go_to_commodity_management_page(self):
        """
        进入商品管理菜单页
        :return:
        """
        try:
            log.info('进入商品管理菜单页')
            self.open_menu_2('商品管理')
        except Exception as e:
            log.error(f'进入商品管理菜单页，异常为{e}')
            raise e

    @allure.step('管理台打开指定的菜单')
    def open_menu(self, platform, name):
        """
        管理台打开指定的菜单
        platform:平台
        menu_name:菜单名
        """
        try:
            # 当前测试请求的url
            test_url = self.driver.current_url
            # 获取host
            url = test_url.split('.com')[0] + '.com'
            dict_menu = {}  # 放菜单和路由对应关系
            if platform == 'yg':
                file = BASE_DIR + '/TestCase/test_huawang_yg/test_ht_ui/test_menu_page/test_huawang_yg_ht_menu_page.xlsx'
                data, ids = read_all_caseexcel(file)
                # 取路由
                for i in range(len(data)):
                    menu_name = data[i][-1]  # 菜单名
                    path_route = data[i][1]  # 路由
                    dict_menu[menu_name] = path_route
                log.info(f'打开的菜单路径为：{url + dict_menu[name]}')
                self.driver.get(url + dict_menu[name])

            elif platform == 'yk':
                test_url = self.driver.current_url
                # 获取host
                url = test_url.split('/#')[0]
                # url = test_url.split('.com')[0] + '.com'
                file = BASE_DIR + '/TestCase/test_huawang_yk/test_ht_ui/test_menu_page/test_huawang_yk_ht_menu_page.xlsx'
                data, ids = read_all_caseexcel(file)
                # print(data,ids)
                # 取路由
                # print(len(data))
                for i in range(len(data)):
                    menu_name = data[i][-1]  # 菜单名
                    # print(menu_name)
                    path_route = data[i][1]  # 路由
                    # print(path_route)
                    dict_menu[menu_name] = path_route
                self.driver.get(url + dict_menu[name])

            elif platform == 'yc':
                file = BASE_DIR + '/TestCase/test_huawang_yc/test_ht_ui/test_menu_page/test_huawang_yc_ht_menu_page.xlsx'
                data, ids = read_all_caseexcel(file)
                # 取路由
                for i in range(len(data)):
                    menu_name = data[i][-1]  # 菜单名
                    path_route = data[i][1]  # 路由
                    dict_menu[menu_name] = path_route
                self.driver.get(url + dict_menu[name])

        except Exception as e:
            log.error(f'打开菜单失败，异常为{e}')
            raise e

    @allure.step('金华商城后台：展开指定菜单')
    def open_menu_2(self, menu_name):
        """
        金华商城后台：展开指定菜单
        """
        try:
            if menu_name in '商品管理 商品分类 商品规格 商品评论':
                sel = self.driver.find_element(By.XPATH, "//*[@class='el-icon-s-goods']/../..")
                open_or_not = sel.get_attribute('class')  # “商品” 菜单是否展开
                if 'is-opened' not in open_or_not:
                    sel.click()
                object_left_click(self.driver, (By.LINK_TEXT, menu_name))
                sleep(1)
            elif menu_name == '用户':
                sel = self.driver.find_element(By.XPATH, "//*[@class='el-icon-user-solid']/../..")
                open_or_not = sel.get_attribute('class')  # “用户” 菜单是否展开
                if 'is-opened' not in open_or_not:
                    sel.click()
            elif menu_name == '内容':
                sel = self.driver.find_element(By.XPATH, "//*[@class='el-icon-s-management']/../..")
                open_or_not = sel.get_attribute('class')  # “内容” 菜单是否展开
                if 'is-opened' not in open_or_not:
                    sel.click()
            elif menu_name == '营销':
                sel = self.driver.find_element(By.XPATH, "//*[@class='el-icon-s-marketing']/../..")
                open_or_not = sel.get_attribute('class')  # “营销” 菜单是否展开
                if 'is-opened' not in open_or_not:
                    sel.click()
            elif menu_name == '优惠券':
                sel = self.driver.find_elements(By.XPATH, "//*[@class='el-submenu__title']/..")
                open_or_not = sel[4].get_attribute('class')
                if 'is-opened' not in open_or_not:  # 判断“优惠券”是否展开
                    sel[4].click()
            elif menu_name == '积分':
                sel = self.driver.find_elements(By.XPATH, "//*[@class='el-submenu__title']/..")
                open_or_not = sel[5].get_attribute('class')
                if 'is-opened' not in open_or_not:  # 判断“积分”是否展开
                    sel[5].click()
            elif menu_name == '分销':
                sel = self.driver.find_element(By.XPATH, "//*[@class='el-icon-s-check']/../..")
                open_or_not = sel.get_attribute('class')  # “分销” 菜单是否展开
                if 'is-opened' not in open_or_not:
                    sel.click()
            elif menu_name == '财务':
                sel = self.driver.find_element(By.XPATH, "//*[@class='el-icon-s-finance']/../..")
                open_or_not = sel.get_attribute('class')  # “财务” 菜单是否展开
                if 'is-opened' not in open_or_not:
                    sel.click()
            elif menu_name == '财务记录':
                sel = self.driver.find_elements(By.XPATH, "//*[@class='el-submenu__title']/..")
                open_or_not = sel[8].get_attribute('class')
                if 'is-opened' not in open_or_not:  # 判断“财务记录”是否展开
                    sel[8].click()
            elif menu_name == '设置':
                sel = self.driver.find_element(By.XPATH, "//*[@class='el-icon-s-tools']/../..")
                open_or_not = sel.get_attribute('class')  # “设置” 菜单是否展开
                if 'is-opened' not in open_or_not:
                    sel.click()
            elif menu_name == '管理权限':
                sel = self.driver.find_elements(By.XPATH, "//*[@class='el-submenu__title']/..")
                open_or_not = sel[10].get_attribute('class')
                if 'is-opened' not in open_or_not:  # 判断“设置-管理权限”是否展开
                    sel[10].click()
            elif menu_name == '维护':
                sel = self.driver.find_element(By.XPATH, "//*[@class='el-icon-s-open']/../..")
                open_or_not = sel.get_attribute('class')  # “维护” 菜单是否展开
                if 'is-opened' not in open_or_not:
                    sel.click()
            elif menu_name == 'x':
                pass
            elif menu_name == 'x':
                pass
            elif menu_name == 'x':
                pass
            elif menu_name == 'x':
                pass

        except Exception as e:
            log.error(f'打开菜单失败，异常为{e}')
            raise e


if __name__ == '__main__':
    JHHTHomePage().open_menu('yc', '商品规格')
    # JHHTHomePage().open_menu('yk', '商品规格')
